package com.java.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.java.domain.event.ActivityAdminEvent;
import com.java.domain.event.ActivityCoachEvent;
import com.java.domain.event.ActivityCourseEvent;
import com.java.domain.event.ActivityRoomEvent;
import com.java.domain.event.ActivitySiteEvent;
import com.java.domain.event.ActivitySportEvent;
import com.java.service.AdminService;
import com.java.service.CoachService;
import com.java.service.CourseService;
import com.java.service.RoomService;
import com.java.service.SiteService;
import com.java.service.SportService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@Controller
@RequestMapping("/admin")
public class AdminIndexController {
	
	@Autowired private AdminService adminService;
	@Autowired private SportService sportService;
	@Autowired private CoachService coachService;
	@Autowired private RoomService roomService;
	@Autowired private CourseService courseService;
	@Autowired private SiteService siteService;

	@RequestMapping(value="/signin", method = RequestMethod.GET)
	public String signin() {
		if(ServletUtil.adminIsLogin()) {
			return "redirect:/admin/account";
		}
		return "admin/signin";
	}

	@RequestMapping(value="/logon", method = RequestMethod.POST)
	public String logon(String account, String password, Model model) {
		if("".equals(account)) {
			model.addAttribute("error", "Account is empty");
			return "admin/signin";
		}
		if("".equals(password)) {
			model.addAttribute("error", "Password is empty");
			return "admin/signin";
		}
		BaseRespone res = adminService.login(account, password);
		if(res.getStatus() == 500) {
			model.addAttribute("error", res.getMsg());
			return "admin/signin";
		}
		return "redirect:/admin/account";
	}

	@RequestMapping(value="/logout", method = RequestMethod.GET)
	public String logout() {
		ServletUtil.getSession().invalidate();
		return "admin/signin";
	}

	@RequestMapping(value="/", method = RequestMethod.GET)
	public String index() {
		return "admin/account";
	}

	@RequestMapping(value="/account", method = RequestMethod.GET)
	public String account(Model model) {
		ActivityAdminEvent admin = adminService.getOne(ServletUtil.getAdminId());
		model.addAttribute("admin", admin);
		return "admin/account";
	}
	@RequestMapping(value="/data", method = RequestMethod.GET)
	public String data(String start, String end, Model model) {
		List<String> dlist = MyTools.last7Date();
		if(MyTools.isEmpty(start)) {
			start = dlist.get(0);
		}
		if(MyTools.isEmpty(end)) {
			end = dlist.get(dlist.size()-1);
		}
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		return "admin/data";
	}
	@RequestMapping(value="/user", method = RequestMethod.GET)
	public String user() {
		return "admin/user";
	}
	@RequestMapping(value="/coach", method = RequestMethod.GET)
	public String coach() {
		return "admin/coach";
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/course", method = RequestMethod.GET)
	public String course(Model model) {
		List<ActivityCoachEvent> coachs = new ArrayList<ActivityCoachEvent>();
		TableRespone ctable = coachService.getList(0, 0, 1);
		if(ctable.getTotal() > 0) {
			coachs = (List<ActivityCoachEvent>) ctable.getRows();
		}
		model.addAttribute("coachs", coachs);
		return "admin/course";
	}
	@RequestMapping(value="/room", method = RequestMethod.GET)
	public String room() {
		return "admin/room";
	}
	@RequestMapping(value="/site", method = RequestMethod.GET)
	public String site() {
		return "admin/site";
	}
	@RequestMapping(value="/law", method = RequestMethod.GET)
	public String law() {
		return "admin/law";
	}
	@RequestMapping(value="/admin", method = RequestMethod.GET)
	public String admin() {
		return "admin/admin";
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/sport", method = RequestMethod.GET)
	public String sport(Model model) {
		List<ActivitySiteEvent> sites = new ArrayList<ActivitySiteEvent>();
		List<ActivityRoomEvent> rooms = new ArrayList<ActivityRoomEvent>();
		List<ActivityCourseEvent> courses = new ArrayList<ActivityCourseEvent>();
		TableRespone stable = siteService.getList(0, 0, 0);
		if(stable.getTotal() > 0) {
			sites = (List<ActivitySiteEvent>) stable.getRows();
		}
		TableRespone rtable = roomService.getList(0, 0, 0);
		if(rtable.getTotal() > 0) {
			rooms = (List<ActivityRoomEvent>) rtable.getRows();
		}
		TableRespone ctable = courseService.getList(0, 0);
		if(ctable.getTotal() > 0) {
			courses = (List<ActivityCourseEvent>) ctable.getRows();
		}
		model.addAttribute("sites", sites);
		model.addAttribute("rooms", rooms);
		model.addAttribute("courses", courses);
		return "admin/sport";
	}
	@RequestMapping(value="/review", method = RequestMethod.GET)
	public String review(String sportId, Model model) {
		model.addAttribute("sportId", sportId);
		ActivitySportEvent sport = sportService.getOne(sportId);
		model.addAttribute("sport", sport);
		return "admin/review";
	}
	@RequestMapping(value="/record", method = RequestMethod.GET)
	public String record() {
		return "admin/record";
	}
	@RequestMapping(value="/slot", method = RequestMethod.GET)
	public String slot(String sportId, Model model) {
		model.addAttribute("sportId", sportId);
		ActivitySportEvent sport = sportService.getOne(sportId);
		model.addAttribute("sport", sport);
		return "admin/slot";
	}
}
